package com.demo.testui;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

//本页给出了方法1失效的解决方法
//当view显示的时候才采用方法1

public class ActivitySolve1_ok extends Activity {
    String TAG = Tags.solve1;
    View mView;
    MyView mMyView;
    MyViewGroup mMyViewGroup;
    float percent = Constant.PERCENT_SHOW;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test_view_size_solve1_limit);

        //重置自定义的View
        MyView.percent = Constant.PERCENT_DEFAULT;
        MyViewGroup.percent = Constant.PERCENT_DEFAULT;

        init();
    }

    Handler handler = new Handler(Looper.getMainLooper());

    private void init() {
        mView = findViewById(R.id.click_area_view);
        mMyView = findViewById(R.id.click_area_myview);
        mMyViewGroup = findViewById(R.id.click_area_myviewgroup);
        mView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(v.getContext(), "View", Toast.LENGTH_SHORT).show();
            }
        });
        mMyView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(v.getContext(), "MyView", Toast.LENGTH_SHORT).show();
            }
        });
        mMyViewGroup.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(v.getContext(), "MyViewGroup", Toast.LENGTH_SHORT).show();
            }
        });
//        setClickSize(mView, percent, "  View     ");
//        setClickSize(mMyView, percent, "MyView     ");
//        setClickSize(mMyViewGroup, percent, "MyViewGroup");

        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                mView.setVisibility(View.VISIBLE);
                mMyView.setVisibility(View.VISIBLE);
                mMyViewGroup.setVisibility(View.VISIBLE);

//                handler.postDelayed(new Runnable() {
//                    @Override
//                    public void run() {
                        setClickSize(mView, percent, "  View     ");
                        setClickSize(mMyView, percent, "MyView     ");
                        setClickSize(mMyViewGroup, percent, "MyViewGroup");
//                    }
//                }, 2000);

            }
        }, 8000);
    }

    private void setClickSize(final View view, final float percent, final String tag) {
        /** 方法1 使用view.post(...) **/
        view.post(new Runnable() {
            @Override
            public void run() {
                Log.e(TAG, tag + "size:[" + view.getMeasuredWidth() + "," + view.getMeasuredHeight() + "]");
                ViewGroup.LayoutParams btnLayoutParams = (ViewGroup.LayoutParams) view.getLayoutParams();
                btnLayoutParams.width = (int) (view.getMeasuredWidth() * percent);
                btnLayoutParams.height = (int) (view.getMeasuredHeight() * percent);
                view.setLayoutParams(btnLayoutParams);
            }
        });
    }
}